from django.http import JsonResponse
"""
定义 Mixin扩展类
实现 判断用户是否登陆
"""


# 定义装饰函数
def my_decorator(view):
    # 内层函数
    def wrapper(request, *args, **kwargs):
        # 判断用户是否登陆
        if request.user.is_authenticated:
            # 登陆
            # print('登陆')
            return view(request, *args, **kwargs)

        else:
            # 未登陆
            return JsonResponse({"errno": "4101", "errmsg": "用户未登录"})
    return wrapper


class LoginRequiredMixin(object):

    # 重写as_view方法
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        return my_decorator(view)
